#!/bin/sh
#
# chkconfig: - 70 45
# description: The LogStash collection agent
# processname: logstash-agent
# config: /etc/sysconfig/logstash

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

# Defaults. Override in /etc/sysconfig/logstash-agent
OPTIONS=""
DEBUG=false
DIR=/opt/logstash
CONFIG=$DIR/etc/logstash-agent.yaml
LOGFILE=/var/log/logstash-agent.log

if [ -f /etc/sysconfig/logstash-agent ]; then 
	. /etc/sysconfig/logstash-agent
fi

RETVAL=0
prog="logstash-agent"

start () {
	echo -n $"Starting $prog: "

	if $DEBUG; then
		OPTIONS="$OPTIONS -d"
	fi
        OPTIONS="$OPTIONS --pidfile /var/run/logstash-agent.pid"
        OPTIONS="$OPTIONS --logfile $LOGFILE"
        OPTIONS="$OPTIONS $CONFIG"

	daemon --pidfile /var/run/logstash-agent.pid \
	       $DIR/bin/logstash-agent $OPTIONS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/logstash-agent
}

stop () {
	echo -n $"Stopping $prog: "
	killproc -p /var/run/logstash-agent.pid logstash-agent
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ] ; then
	    rm -f /var/lock/subsys/logstash-agent
	    rm -f /var/run/logstash-agent.pid
	fi
}

restart () {
        stop
        start
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status -p /var/run/logstash-agent.pid logstash-agent
	;;
  restart|reload|force-reload)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/logstash-agent ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
	exit 1
esac

exit $?
